<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-地区`"
		width="800px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		append-to-body
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
			<el-row ref="box1">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="地区编号" prop="code">
						<el-input v-model="ruleForm.code" size="medium" class="width-100-percent" placeholder="地区编号"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="地区名称" prop="name">
						<el-input v-model="ruleForm.name" size="medium" class="width-100-percent" placeholder="地区名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="备注">
						<el-input v-model="ruleForm.remark" type="textarea" :rows="2" placeholder="名称" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="涵盖城市">
						<div ref="box2" class="flex-between-center">
							<el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="checkboxCityDialogState = true">添加城市</el-button>
							<div class="flex-end-center">
								<div class="item-gap"><el-button size="mini" type="default">合计：{{ ruleForm.cityList ? ruleForm.cityList.length : 0 }}个</el-button></div>
								<el-button size="mini" icon="el-icon-delete" type="danger" plain @click="ruleForm.cityList = []">清空</el-button>
							</div>
						</div>
						<div class="city-box" :style="{ maxHeight: `${dialogTableHeight - 30}px` }">
							<div
								v-for="(item, index) in ruleForm.cityList"
								:key="index"
								class="city-item"
							>
								<el-tag :type="item.openId ? 'primary' : 'info'" closable class="width-100-percent flex-between-center" @close="onDeleteBtn(index)">
									<span>{{ item.city }}</span>
								</el-tag>
							</div>
							<div v-if="!ruleForm.cityList || !ruleForm.cityList.length" class="flex-center width-100-percent">
								<span>暂无</span>
							</div>
						</div>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<checkbox-city-dialog
			:default-select-rows="ruleForm.cityList"
			check-key-name="id"
			:dialog-state="checkboxCityDialogState"
			@on-visible-change="onVisibleChangeOfCheckboxCityDialog"
			@on-confirm="onConfirmBtnOfCheckboxEmpDialog"
		></checkbox-city-dialog>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import {
        systemAreaCreate,
        systemAreaUpdate,
        systemAreaGet
    } from '../../../api/price/areaConfig';
    import checkboxCityDialog from './checkbox-city-dialog';
    export default {
        components: {
            checkboxCityDialog
        },
        props: {
            // 页面权限的前缀
            permissionPrefix: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                showDialog: false,
                showLoading: true,
                saveBtnLoading: false,
                ruleForm: {
                    code: null,
                    name: null,
                    remark: '',
                    cityList: [],
                    auditState: 1
                },
                rules: {
                    code: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }]
                },
                checkboxCityDialogState: false
            };
        },
        methods: {
            onDeleteBtn(idx) {
                this.ruleForm.cityList.splice(idx, 1);
            },
            onVisibleChangeOfCheckboxCityDialog(e) {
                this.checkboxCityDialogState = e;
            },
            onConfirmBtnOfCheckboxEmpDialog(arr) {
                arr.forEach(x => {
                    this.ruleForm.cityList.push(x);
                });
                this.checkboxCityDialogState = false;
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                // 新增
                if (!this.dataId) {
                    this.ruleForm.id = null;
                    systemAreaCreate(this.ruleForm).then(res => {
                        this.saveBtnLoading = false;
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'save');
                            this.$emit('on-finish');
                        }
                    });
                    return;
                }

                // 编辑
                systemAreaUpdate(this.ruleForm).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish');
                    }
                });
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return systemAreaGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res || {};
                        resData.cityList = resData.cityList || [];
                        this.ruleForm = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped lang="scss">
    .city-box {
        border: solid 1px #dcdee2;
        border-radius: 4px;
        overflow: auto;
        padding: 8px 10px;
        display: flex;
        flex-wrap: wrap;
        box-sizing: border-box;
        .city-item {
            width: calc(100% / 5);
            padding: 2px;
            flex-shrink: 0;
            box-sizing: border-box;
        }
    }
</style>
